'''统计大中小区域的数量'''

import os
from tqdm import tqdm
import shutil
import xml.etree.ElementTree as ET

ann_path_origin = r'/home/xys/datasets/dataset_che/dataset-larger/Annotations/'        #要提取的ann标签文件路径
inputsize = 512
small = 0
Midle = 0
larger = 0

for xml_file_name in tqdm(os.listdir(ann_path_origin),ncols=150):
    file_name = xml_file_name[:-4]
    xml_file = open(ann_path_origin+xml_file_name)      #打开xml文件
    tree = ET.parse(xml_file)
    root = tree.getroot()                               #得到根
    image_width = int(root.find('size').find('width').text)
    image_height = int(root.find('size').find('height').text)
    for obj in root.iter('object'):
        xmlbox = obj.find('bndbox')                 #找到boundingbox标签
        xmin = int(float(xmlbox.find('xmin').text))
        ymin = int(float(xmlbox.find('ymin').text))
        xmax = int(float(xmlbox.find('xmax').text))
        ymax = int(float(xmlbox.find('ymax').text))
        if ((xmax-xmin) * (ymax-ymin))/(image_width*image_height) < 1024/(inputsize*inputsize):
            small = small+1
            print(xml_file_name)
        elif ((xmax-xmin) * (ymax-ymin))/(image_width*image_height) > 9216/(inputsize*inputsize):
            larger = larger+1
        else:
            Midle = Midle + 1
    xml_file.close()
print(f"---------有{small}个小元素,"
      f"---------有{Midle}个中元素,"
      f"---------有{larger}个大元素-"
      f"-----------结束-------------")

